package cheme.ProcessEditor.Table;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;

import cheme.ProcessEditor.LeafFigureNavigator;
import cheme.ProcessEditor.Parts.AbstractParentPart;
import cheme.ProcessEditor.Util.SelectionHelper;

public abstract class AbstractListPart extends AbstractParentPart
{	
	@Override
	protected void addChild(EditPart child, int index)
	{
		/*
		 * If there was a specific field selected in the last component desc,
		 * select the same field in the new component desc. 
		 */

		super.addChild(child, index);
		
		GraphicalEditPart navigateFrom = SelectionHelper.getFirstLeaf((GraphicalEditPart)child);
		
		if(null != navigateFrom)
		{
			LeafFigureNavigator.navigateTo(navigateFrom);
		}
	}
	
	@Override
	protected void removeChild(EditPart child)
	{
		/*
		 * If the child being remove had primary focus, we navigate to the 
		 * nearest neighbor above. This is a nicety for table-like editing in the
		 * stream descriptor.
		 */

		GraphicalEditPart navigateFrom = SelectionHelper.getFirstLeaf((GraphicalEditPart)child); 
		
		if(null != navigateFrom)
		{
			LeafFigureNavigator.navigateNearestSibling(navigateFrom, PositionConstants.NORTH);
		}
		
		super.removeChild(child);
	}
}
